﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WQDSkin.Forms.Component;

namespace WQDSkin.Forms.Controls
{
    public class WQDTextBox : TextBox, IForm
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
             
            switch (this.Type)
            {
                case TextBoxType.Number:
                case TextBoxType.Decimal:
                    InputMethod.SetIsInputMethodEnabled(this, false);
                    break; 
            } 
            
        }

        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);

            if (this.Required && this.Focus()) //只有获取焦点时才验证
            {
                Verifiy();
            }
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            switch (this.Type)
            {
                case TextBoxType.Number:
                    Regex re = new Regex("[^0-9]+");
                    e.Handled = re.IsMatch(e.Text);
                    break;
                case TextBoxType.Decimal: 
                    break;
                default: 
                    base.OnPreviewTextInput(e); 
                    break;
            }
             
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        { 
            switch (this.Type)
            {
                case TextBoxType.Number:
                    if (e.Key == Key.Space)
                        e.Handled = true;
                    break;
                case TextBoxType.Decimal:
                    if ((e.Key == Key.OemPeriod && this.Text.Contains(".")) || e.Key == Key.OemPeriod && string.IsNullOrWhiteSpace(this.Text))
                    {
                        e.Handled = true;
                        return;
                    };

                    if (!((e.Key >= Key.D0 && e.Key <= Key.D9)
                        || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.OemPeriod
                        || e.Key == Key.Tab || e.Key == Key.OemBackTab
                        || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
                        || (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.A))
                    {
                        if (e.KeyboardDevice.Modifiers != ModifierKeys.Control)
                        {
                            e.Handled = true;
                        }
                    }
                    break;
                default:
                    base.OnPreviewKeyDown(e);
                    break;
            }
        }

        public bool Required
        {
            get { return (bool)GetValue(RequiredProperty); }
            set { SetValue(RequiredProperty, value); }
        } 
        public static readonly DependencyProperty RequiredProperty =
            DependencyProperty.Register("Required", typeof(bool), typeof(WQDTextBox), new PropertyMetadata(false));
         
        public bool HasError
        {
            get { return (bool)GetValue(HasErrorProperty); }
            set { SetValue(HasErrorProperty, value); }
        } 
        public static readonly DependencyProperty HasErrorProperty =
            DependencyProperty.Register("HasError", typeof(bool), typeof(WQDTextBox), new PropertyMetadata(false));


        /// <summary>
        /// 控件输入类型
        /// </summary>
        public TextBoxType Type
        {
            get { return (TextBoxType)GetValue(TypeProperty); }
            set { SetValue(TypeProperty, value); }
        }
             
        public static readonly DependencyProperty TypeProperty =
            DependencyProperty.Register("Type", typeof(TextBoxType), 
                typeof(WQDTextBox), 
                new PropertyMetadata(null));



        public bool Verifiy()
        {
            bool isNull = string.IsNullOrEmpty(this.Text); 
            this.HasError = isNull;
            return !isNull;
        }

        public string Hint
        {
            get { return (string)GetValue(HintProperty); }
            set { SetValue(HintProperty, value); }
        }
        public static readonly DependencyProperty HintProperty =
            DependencyProperty.Register("Hint", typeof(string), typeof(WQDTextBox), new PropertyMetadata(""));
         
    }
}
